home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume24 / gnudiff1.15 / part05 < prev    next >
Encoding:
Internet Message Format  |  1991-03-05  |  28.1 KB

  1. Subject:  v24i020:  GNU Diff, version 1.15, Part05/08
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 30fba46a 6ca683b6 aeb8b999 0730224a
  5.  
  6. Submitted-by: Paul Eggert <eggert@twinsun.com>
  7. Posting-number: Volume 24, Issue 20
  8. Archive-name: gnudiff1.15/part05
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 5 (of 8)."
  17. # Contents:  analyze.c
  18. # Wrapped by eggert@ata on Mon Jan  7 11:25:30 1991
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'analyze.c' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'analyze.c'\"
  22. else
  23. echo shar: Extracting \"'analyze.c'\" \(26295 characters\)
  24. sed "s/^X//" >'analyze.c' <<'END_OF_FILE'
  25. X/* Analyze file differences for GNU DIFF.
  26. X   Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  27. X
  28. XThis file is part of GNU DIFF.
  29. X
  30. XGNU DIFF is free software; you can redistribute it and/or modify
  31. Xit under the terms of the GNU General Public License as published by
  32. Xthe Free Software Foundation; either version 1, or (at your option)
  33. Xany later version.
  34. X
  35. XGNU DIFF is distributed in the hope that it will be useful,
  36. Xbut WITHOUT ANY WARRANTY; without even the implied warranty of
  37. XMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. XGNU General Public License for more details.
  39. X
  40. XYou should have received a copy of the GNU General Public License
  41. Xalong with GNU DIFF; see the file COPYING.  If not, write to
  42. Xthe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  43. X
  44. X/* The basic algorithm is described in: 
  45. X   "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
  46. X   Algorithmica Vol. 1 No. 2, 1986, p 251.  */
  47. X
  48. X#include "diff.h"
  49. X
  50. Xstruct change *find_change ();
  51. Xvoid finish_output ();
  52. Xvoid print_context_header ();
  53. Xvoid print_context_script ();
  54. Xvoid print_ed_script ();
  55. Xvoid print_ifdef_script ();
  56. Xvoid print_normal_script ();
  57. Xvoid print_rcs_script ();
  58. Xvoid pr_forward_ed_script ();
  59. Xvoid setup_output ();
  60. X
  61. Xextern int no_discards;
  62. X
  63. Xstatic int *xvec, *yvec;    /* Vectors being compared. */
  64. Xstatic int *fdiag;        /* Vector, indexed by diagonal, containing
  65. X                   the X coordinate of the point furthest
  66. X                   along the given diagonal in the forward
  67. X                   search of the edit matrix. */
  68. Xstatic int *bdiag;        /* Vector, indexed by diagonal, containing
  69. X                   the X coordinate of the point furthest
  70. X                   along the given diagonal in the backward
  71. X                   search of the edit matrix. */
  72. X
  73. X/* Find the midpoint of the shortest edit script for a specified
  74. X   portion of the two files.
  75. X
  76. X   We scan from the beginnings of the files, and simultaneously from the ends,
  77. X   doing a breadth-first search through the space of edit-sequence.
  78. X   When the two searches meet, we have found the midpoint of the shortest
  79. X   edit sequence.
  80. X
  81. X   The value returned is the number of the diagonal on which the midpoint lies.
  82. X   The diagonal number equals the number of inserted lines minus the number
  83. X   of deleted lines (counting only lines before the midpoint).
  84. X   The edit cost is stored into *COST; this is the total number of
  85. X   lines inserted or deleted (counting only lines before the midpoint).
  86. X
  87. X   This function assumes that the first lines of the specified portions
  88. X   of the two files do not match, and likewise that the last lines do not
  89. X   match.  The caller must trim matching lines from the beginning and end
  90. X   of the portions it is going to specify.
  91. X
  92. X   Note that if we return the "wrong" diagonal value, or if
  93. X   the value of bdiag at that diagonal is "wrong",
  94. X   the worst this can do is cause suboptimal diff output.
  95. X   It cannot cause incorrect diff output.  */
  96. X
  97. Xstatic int
  98. Xdiag (xoff, xlim, yoff, ylim, cost)
  99. X     int xoff, xlim, yoff, ylim;
  100. X     int *cost;
  101. X{
  102. X  int *const fd = fdiag;    /* Give the compiler a chance. */
  103. X  int *const bd = bdiag;    /* Additional help for the compiler. */
  104. X  int *const xv = xvec;        /* Still more help for the compiler. */
  105. X  int *const yv = yvec;        /* And more and more . . . */
  106. X  const int dmin = xoff - ylim;    /* Minimum valid diagonal. */
  107. X  const int dmax = xlim - yoff;    /* Maximum valid diagonal. */
  108. X  const int fmid = xoff - yoff;    /* Center diagonal of top-down search. */
  109. X  const int bmid = xlim - ylim;    /* Center diagonal of bottom-up search. */
  110. X  int fmin = fmid, fmax = fmid;    /* Limits of top-down search. */
  111. X  int bmin = bmid, bmax = bmid;    /* Limits of bottom-up search. */
  112. X  int c;            /* Cost. */
  113. X  int odd = fmid - bmid & 1;    /* True if southeast corner is on an odd
  114. X                   diagonal with respect to the northwest. */
  115. X
  116. X  fd[fmid] = xoff;
  117. X  bd[bmid] = xlim;
  118. X
  119. X  for (c = 1;; ++c)
  120. X    {
  121. X      int d;            /* Active diagonal. */
  122. X      int big_snake = 0;
  123. X
  124. X      /* Extend the top-down search by an edit step in each diagonal. */
  125. X      fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
  126. X      fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
  127. X      for (d = fmax; d >= fmin; d -= 2)
  128. X    {
  129. X      int x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
  130. X
  131. X      if (tlo >= thi)
  132. X        x = tlo + 1;
  133. X      else
  134. X        x = thi;
  135. X      oldx = x;
  136. X      y = x - d;
  137. X      while (x < xlim && y < ylim && xv[x] == yv[y])
  138. X        ++x, ++y;
  139. X      if (x - oldx > 20)
  140. X        big_snake = 1;
  141. X      fd[d] = x;
  142. X      if (odd && bmin <= d && d <= bmax && bd[d] <= fd[d])
  143. X        {
  144. X          *cost = 2 * c - 1;
  145. X          return d;
  146. X        }
  147. X    }
  148. X
  149. X      /* Similar extend the bottom-up search. */
  150. X      bmin > dmin ? bd[--bmin - 1] = INT_MAX : ++bmin;
  151. X      bmax < dmax ? bd[++bmax + 1] = INT_MAX : --bmax;
  152. X      for (d = bmax; d >= bmin; d -= 2)
  153. X    {
  154. X      int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
  155. X
  156. X      if (tlo < thi)
  157. X        x = tlo;
  158. X      else
  159. X        x = thi - 1;
  160. X      oldx = x;
  161. X      y = x - d;
  162. X      while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
  163. X        --x, --y;
  164. X      if (oldx - x > 20)
  165. X        big_snake = 1;
  166. X      bd[d] = x;
  167. X      if (!odd && fmin <= d && d <= fmax && bd[d] <= fd[d])
  168. X        {
  169. X          *cost = 2 * c;
  170. X          return d;
  171. X        }
  172. X    }
  173. X
  174. X      /* Heuristic: check occasionally for a diagonal that has made
  175. X     lots of progress compared with the edit distance.
  176. X     If we have any such, find the one that has made the most
  177. X     progress and return it as if it had succeeded.
  178. X
  179. X     With this heuristic, for files with a constant small density
  180. X     of changes, the algorithm is linear in the file size.  */
  181. X
  182. X      if (c > 200 && big_snake && heuristic)
  183. X    {
  184. X      int best;
  185. X      int bestpos;
  186. X
  187. X      best = 0;
  188. X      for (d = fmax; d >= fmin; d -= 2)
  189. X        {
  190. X          int dd = d - fmid;
  191. X          if ((fd[d] - xoff)*2 - dd > 12 * (c + (dd > 0 ? dd : -dd)))
  192. X        {
  193. X          if (fd[d] * 2 - dd > best
  194. X              && fd[d] - xoff > 20
  195. X              && fd[d] - d - yoff > 20)
  196. X            {
  197. X              int k;
  198. X              int x = fd[d];
  199. X
  200. X              /* We have a good enough best diagonal;
  201. X             now insist that it end with a significant snake.  */
  202. X              for (k = 1; k <= 20; k++)
  203. X            if (xvec[x - k] != yvec[x - d - k])
  204. X              break;
  205. X
  206. X              if (k == 21)
  207. X            {
  208. X              best = fd[d] * 2 - dd;
  209. X              bestpos = d;
  210. X            }
  211. X            }
  212. X        }
  213. X        }
  214. X      if (best > 0)
  215. X        {
  216. X          *cost = 2 * c - 1;
  217. X          return bestpos;
  218. X        }
  219. X
  220. X      best = 0;
  221. X      for (d = bmax; d >= bmin; d -= 2)
  222. X        {
  223. X          int dd = d - bmid;
  224. X          if ((xlim - bd[d])*2 + dd > 12 * (c + (dd > 0 ? dd : -dd)))
  225. X        {
  226. X          if ((xlim - bd[d]) * 2 + dd > best
  227. X              && xlim - bd[d] > 20
  228. X              && ylim - (bd[d] - d) > 20)
  229. X            {
  230. X              /* We have a good enough best diagonal;
  231. X             now insist that it end with a significant snake.  */
  232. X              int k;
  233. X              int x = bd[d];
  234. X
  235. X              for (k = 0; k < 20; k++)
  236. X            if (xvec[x + k] != yvec[x - d + k])
  237. X              break;
  238. X              if (k == 20)
  239. X            {
  240. X              best = (xlim - bd[d]) * 2 + dd;
  241. X              bestpos = d;
  242. X            }
  243. X            }
  244. X        }
  245. X        }
  246. X      if (best > 0)
  247. X        {
  248. X          *cost = 2 * c - 1;
  249. X          return bestpos;
  250. X        }
  251. X    }
  252. X    }
  253. X}
  254. X
  255. X/* Compare in detail contiguous subsequences of the two files
  256. X   which are known, as a whole, to match each other.
  257. X
  258. X   The results are recorded in the vectors files[N].changed_flag, by
  259. X   storing a 1 in the element for each line that is an insertion or deletion.
  260. X
  261. X   The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
  262. X
  263. X   Note that XLIM, YLIM are exclusive bounds.
  264. X   All line numbers are origin-0 and discarded lines are not counted.  */
  265. X
  266. Xstatic void
  267. Xcompareseq (xoff, xlim, yoff, ylim)
  268. X     int xoff, xlim, yoff, ylim;
  269. X{
  270. X  /* Slide down the bottom initial diagonal. */
  271. X  while (xoff < xlim && yoff < ylim && xvec[xoff] == yvec[yoff])
  272. X    ++xoff, ++yoff;
  273. X  /* Slide up the top initial diagonal. */
  274. X  while (xlim > xoff && ylim > yoff && xvec[xlim - 1] == yvec[ylim - 1])
  275. X    --xlim, --ylim;
  276. X  
  277. X  /* Handle simple cases. */
  278. X  if (xoff == xlim)
  279. X    while (yoff < ylim)
  280. X      files[1].changed_flag[files[1].realindexes[yoff++]] = 1;
  281. X  else if (yoff == ylim)
  282. X    while (xoff < xlim)
  283. X      files[0].changed_flag[files[0].realindexes[xoff++]] = 1;
  284. X  else
  285. X    {
  286. X      int c, d, f, b;
  287. X
  288. X      /* Find a point of correspondence in the middle of the files.  */
  289. X
  290. X      d = diag (xoff, xlim, yoff, ylim, &c);
  291. X      f = fdiag[d];
  292. X      b = bdiag[d];
  293. X
  294. X      if (c == 1)
  295. X    {
  296. X      /* This should be impossible, because it implies that
  297. X         one of the two subsequences is empty,
  298. X         and that case was handled above without calling `diag'.
  299. X         Let's verify that this is true.  */
  300. X      abort ();
  301. X#if 0
  302. X      /* The two subsequences differ by a single insert or delete;
  303. X         record it and we are done.  */
  304. X      if (d < xoff - yoff)
  305. X        files[1].changed_flag[files[1].realindexes[b - d - 1]] = 1;
  306. X      else
  307. X        files[0].changed_flag[files[0].realindexes[b]] = 1;
  308. X#endif
  309. X    }
  310. X      else
  311. X    {
  312. X      /* Use that point to split this problem into two subproblems.  */
  313. X      compareseq (xoff, b, yoff, b - d);
  314. X      /* This used to use f instead of b,
  315. X         but that is incorrect!
  316. X         It is not necessarily the case that diagonal d
  317. X         has a snake from b to f.  */
  318. X      compareseq (b, xlim, b - d, ylim);
  319. X    }
  320. X    }
  321. X}
  322. X
  323. X/* Discard lines from one file that have no matches in the other file.
  324. X
  325. X   A line which is discarded will not be considered by the actual
  326. X   comparison algorithm; it will be as if that line were not in the file.
  327. X   The file's `realindexes' table maps virtual line numbers
  328. X   (which don't count the discarded lines) into real line numbers;
  329. X   this is how the actual comparison algorithm produces results
  330. X   that are comprehensible when the discarded lines are counted.
  331. X
  332. X   When we discard a line, we also mark it as a deletion or insertion
  333. X   so that it will be printed in the output.  */
  334. X
  335. Xvoid
  336. Xdiscard_confusing_lines (filevec)
  337. X     struct file_data filevec[];
  338. X{
  339. X  unsigned int f, i;
  340. X  char *discarded[2];
  341. X  int *equiv_count[2];
  342. X
  343. X  /* Allocate our results.  */
  344. X  for (f = 0; f < 2; f++)
  345. X    {
  346. X      filevec[f].undiscarded
  347. X    = (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
  348. X      filevec[f].realindexes
  349. X    = (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
  350. X    }
  351. X
  352. X  /* Set up equiv_count[F][I] as the number of lines in file F
  353. X     that fall in equivalence class I.  */
  354. X
  355. X  equiv_count[0] = (int *) xmalloc (filevec[0].equiv_max * sizeof (int));
  356. X  bzero (equiv_count[0], filevec[0].equiv_max * sizeof (int));
  357. X  equiv_count[1] = (int *) xmalloc (filevec[1].equiv_max * sizeof (int));
  358. X  bzero (equiv_count[1], filevec[1].equiv_max * sizeof (int));
  359. X
  360. X  for (i = 0; i < filevec[0].buffered_lines; ++i)
  361. X    ++equiv_count[0][filevec[0].equivs[i]];
  362. X  for (i = 0; i < filevec[1].buffered_lines; ++i)
  363. X    ++equiv_count[1][filevec[1].equivs[i]];
  364. X
  365. X  /* Set up tables of which lines are going to be discarded.  */
  366. X
  367. X  discarded[0] = (char *) xmalloc (filevec[0].buffered_lines);
  368. X  discarded[1] = (char *) xmalloc (filevec[1].buffered_lines);
  369. X  bzero (discarded[0], filevec[0].buffered_lines);
  370. X  bzero (discarded[1], filevec[1].buffered_lines);
  371. X
  372. X  /* Mark to be discarded each line that matches no line of the other file.
  373. X     If a line matches many lines, mark it as provisionally discardable.  */
  374. X
  375. X  for (f = 0; f < 2; f++)
  376. X    {
  377. X      unsigned int end = filevec[f].buffered_lines;
  378. X      char *discards = discarded[f];
  379. X      int *counts = equiv_count[1 - f];
  380. X      int *equivs = filevec[f].equivs;
  381. X      unsigned int many = 5;
  382. X      unsigned int tem = end / 64;
  383. X
  384. X      /* Multiply MANY by approximate square root of number of lines.
  385. X     That is the threshold for provisionally discardable lines.  */
  386. X      while ((tem = tem >> 2) > 0)
  387. X    many *= 2;
  388. X
  389. X      for (i = 0; i < end; i++)
  390. X    {
  391. X      int nmatch;
  392. X      if (equivs[i] == 0)
  393. X        continue;
  394. X      nmatch = counts[equivs[i]];
  395. X      if (nmatch == 0)
  396. X        discards[i] = 1;
  397. X      else if (nmatch > many)
  398. X        discards[i] = 2;
  399. X    }
  400. X    }
  401. X
  402. X  /* Don't really discard the provisional lines except when they occur
  403. X     in a run of discardables, with nonprovisionals at the beginning
  404. X     and end.  */
  405. X
  406. X  for (f = 0; f < 2; f++)
  407. X    {
  408. X      unsigned int end = filevec[f].buffered_lines;
  409. X      register char *discards = discarded[f];
  410. X
  411. X      for (i = 0; i < end; i++)
  412. X    {
  413. X      /* Cancel provisional discards not in middle of run of discards.  */
  414. X      if (discards[i] == 2)
  415. X        discards[i] = 0;
  416. X      else if (discards[i] != 0)
  417. X        {
  418. X          /* We have found a nonprovisional discard.  */
  419. X          register int j;
  420. X          unsigned int length;
  421. X          unsigned int provisional = 0;
  422. X
  423. X          /* Find end of this run of discardable lines.
  424. X         Count how many are provisionally discardable.  */
  425. X          for (j = i; j < end; j++)
  426. X        {
  427. X          if (discards[j] == 0)
  428. X            break;
  429. X          if (discards[j] == 2)
  430. X            ++provisional;
  431. X        }
  432. X
  433. X          /* Cancel provisional discards at end, and shrink the run.  */
  434. X          while (j > i && discards[j - 1] == 2)
  435. X        discards[--j] = 0, --provisional;
  436. X
  437. X          /* Now we have the length of a run of discardable lines
  438. X         whose first and last are not provisional.  */
  439. X          length = j - i;
  440. X
  441. X          /* If 1/4 of the lines in the run are provisional,
  442. X         cancel discarding of all provisional lines in the run.  */
  443. X          if (provisional * 4 > length)
  444. X        {
  445. X          while (j > i)
  446. X            if (discards[--j] == 2)
  447. X              discards[j] = 0;
  448. X        }
  449. X          else
  450. X        {
  451. X          register unsigned int consec;
  452. X          unsigned int minimum = 1;
  453. X          unsigned int tem = length / 4;
  454. X
  455. X          /* MINIMUM is approximate square root of LENGTH/4.
  456. X             A subrun of two or more provisionals can stand
  457. X             when LENGTH is at least 16.
  458. X             A subrun of 4 or more can stand when LENGTH >= 64.  */
  459. X          while ((tem = tem >> 2) > 0)
  460. X            minimum *= 2;
  461. X          minimum++;
  462. X
  463. X          /* Cancel any subrun of MINIMUM or more provisionals
  464. X             within the larger run.  */
  465. X          for (j = 0, consec = 0; j < length; j++)
  466. X            if (discards[i + j] != 2)
  467. X              consec = 0;
  468. X            else if (minimum == ++consec)
  469. X              /* Back up to start of subrun, to cancel it all.  */
  470. X              j -= consec;
  471. X            else if (minimum < consec)
  472. X              discards[i + j] = 0;
  473. X
  474. X          /* Scan from beginning of run
  475. X             until we find 3 or more nonprovisionals in a row
  476. X             or until the first nonprovisional at least 8 lines in.
  477. X             Until that point, cancel any provisionals.  */
  478. X          for (j = 0, consec = 0; j < length; j++)
  479. X            {
  480. X              if (j >= 8 && discards[i + j] == 1)
  481. X            break;
  482. X              if (discards[i + j] == 2)
  483. X            consec = 0, discards[i + j] = 0;
  484. X              else if (discards[i + j] == 0)
  485. X            consec = 0;
  486. X              else
  487. X            consec++;
  488. X              if (consec == 3)
  489. X            break;
  490. X            }
  491. X
  492. X          /* I advances to the last line of the run.  */
  493. X          i += length - 1;
  494. X
  495. X          /* Same thing, from end.  */
  496. X          for (j = 0, consec = 0; j < length; j++)
  497. X            {
  498. X              if (j >= 8 && discards[i - j] == 1)
  499. X            break;
  500. X              if (discards[i - j] == 2)
  501. X            consec = 0, discards[i - j] = 0;
  502. X              else if (discards[i - j] == 0)
  503. X            consec = 0;
  504. X              else
  505. X            consec++;
  506. X              if (consec == 3)
  507. X            break;
  508. X            }
  509. X        }
  510. X        }
  511. X    }
  512. X    }
  513. X
  514. X  /* Actually discard the lines. */
  515. X  for (f = 0; f < 2; f++)
  516. X    {
  517. X      char *discards = discarded[f];
  518. X      unsigned int end = filevec[f].buffered_lines;
  519. X      unsigned int j = 0;
  520. X      for (i = 0; i < end; ++i)
  521. X    if (no_discards || discards[i] == 0)
  522. X      {
  523. X        filevec[f].undiscarded[j] = filevec[f].equivs[i];
  524. X        filevec[f].realindexes[j++] = i;
  525. X      }
  526. X    else
  527. X      filevec[f].changed_flag[i] = 1;
  528. X      filevec[f].nondiscarded_lines = j;
  529. X    }
  530. X
  531. X  free (discarded[1]);
  532. X  free (discarded[0]);
  533. X  free (equiv_count[1]);
  534. X  free (equiv_count[0]);
  535. X}
  536. X
  537. X/* Adjust inserts/deletes of blank lines to join changes
  538. X   as much as possible.
  539. X
  540. X   We do something when a run of changed lines include a blank
  541. X   line at one end and have an excluded blank line at the other.
  542. X   We are free to choose which blank line is included.
  543. X   `compareseq' always chooses the one at the beginning,
  544. X   but usually it is cleaner to consider the following blank line
  545. X   to be the "change".  The only exception is if the preceding blank line
  546. X   would join this change to other changes.  */
  547. X
  548. Xint inhibit;
  549. X
  550. Xstatic void
  551. Xshift_boundaries (filevec)
  552. X     struct file_data filevec[];
  553. X{
  554. X  int f;
  555. X
  556. X  if (inhibit)
  557. X    return;
  558. X
  559. X  for (f = 0; f < 2; f++)
  560. X    {
  561. X      char *changed = filevec[f].changed_flag;
  562. X      char *other_changed = filevec[1-f].changed_flag;
  563. X      int i = 0;
  564. X      int j = 0;
  565. X      int i_end = filevec[f].buffered_lines;
  566. X      int preceding = -1;
  567. X      int other_preceding = -1;
  568. X
  569. X      while (1)
  570. X    {
  571. X      int start, end, other_start;
  572. X
  573. X      /* Scan forwards to find beginning of another run of changes.
  574. X         Also keep track of the corresponding point in the other file.  */
  575. X
  576. X      while (i < i_end && changed[i] == 0)
  577. X        {
  578. X          while (other_changed[j++])
  579. X        /* Non-corresponding lines in the other file
  580. X           will count as the preceding batch of changes.  */
  581. X        other_preceding = j;
  582. X          i++;
  583. X        }
  584. X
  585. X      if (i == i_end)
  586. X        break;
  587. X
  588. X      start = i;
  589. X      other_start = j;
  590. X
  591. X      while (1)
  592. X        {
  593. X          /* Now find the end of this run of changes.  */
  594. X
  595. X          while (i < i_end && changed[i] != 0) i++;
  596. X          end = i;
  597. X
  598. X          /* If the first changed line matches the following unchanged one,
  599. X         and this run does not follow right after a previous run,
  600. X         and there are no lines deleted from the other file here,
  601. X         then classify the first changed line as unchanged
  602. X         and the following line as changed in its place.  */
  603. X
  604. X          /* You might ask, how could this run follow right after another?
  605. X         Only because the previous run was shifted here.  */
  606. X
  607. X          if (end != i_end
  608. X          && files[f].equivs[start] == files[f].equivs[end]
  609. X          && !other_changed[j]
  610. X          && end != i_end
  611. X          && !((preceding >= 0 && start == preceding)
  612. X               || (other_preceding >= 0
  613. X               && other_start == other_preceding)))
  614. X        {
  615. X          changed[end++] = 1;
  616. X          changed[start++] = 0;
  617. X          ++i;
  618. X          /* Since one line-that-matches is now before this run
  619. X             instead of after, we must advance in the other file
  620. X             to keep in synch.  */
  621. X          ++j;
  622. X        }
  623. X          else
  624. X        break;
  625. X        }
  626. X
  627. X      preceding = i;
  628. X      other_preceding = j;
  629. X    }
  630. X    }
  631. X}
  632. X
  633. X/* Cons an additional entry onto the front of an edit script OLD.
  634. X   LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  635. X   DELETED is the number of lines deleted here from file 0.
  636. X   INSERTED is the number of lines inserted here in file 1.
  637. X
  638. X   If DELETED is 0 then LINE0 is the number of the line before
  639. X   which the insertion was done; vice versa for INSERTED and LINE1.  */
  640. X
  641. Xstatic struct change *
  642. Xadd_change (line0, line1, deleted, inserted, old)
  643. X     int line0, line1, deleted, inserted;
  644. X     struct change *old;
  645. X{
  646. X  struct change *new = (struct change *) xmalloc (sizeof (struct change));
  647. X
  648. X  new->line0 = line0;
  649. X  new->line1 = line1;
  650. X  new->inserted = inserted;
  651. X  new->deleted = deleted;
  652. X  new->link = old;
  653. X  return new;
  654. X}
  655. X
  656. X/* Scan the tables of which lines are inserted and deleted,
  657. X   producing an edit script in reverse order.  */
  658. X
  659. Xstatic struct change *
  660. Xbuild_reverse_script (filevec)
  661. X     struct file_data filevec[];
  662. X{
  663. X  struct change *script = 0;
  664. X  char *changed0 = filevec[0].changed_flag;
  665. X  char *changed1 = filevec[1].changed_flag;
  666. X  int len0 = filevec[0].buffered_lines;
  667. X  int len1 = filevec[1].buffered_lines;
  668. X
  669. X  /* Note that changedN[len0] does exist, and contains 0.  */
  670. X
  671. X  int i0 = 0, i1 = 0;
  672. X
  673. X  while (i0 < len0 || i1 < len1)
  674. X    {
  675. X      if (changed0[i0] || changed1[i1])
  676. X    {
  677. X      int line0 = i0, line1 = i1;
  678. X
  679. X      /* Find # lines changed here in each file.  */
  680. X      while (changed0[i0]) ++i0;
  681. X      while (changed1[i1]) ++i1;
  682. X
  683. X      /* Record this change.  */
  684. X      script = add_change (line0, line1, i0 - line0, i1 - line1, script);
  685. X    }
  686. X
  687. X      /* We have reached lines in the two files that match each other.  */
  688. X      i0++, i1++;
  689. X    }
  690. X
  691. X  return script;
  692. X}
  693. X
  694. X/* Scan the tables of which lines are inserted and deleted,
  695. X   producing an edit script in forward order.  */
  696. X
  697. Xstatic struct change *
  698. Xbuild_script (filevec)
  699. X     struct file_data filevec[];
  700. X{
  701. X  struct change *script = 0;
  702. X  char *changed0 = filevec[0].changed_flag;
  703. X  char *changed1 = filevec[1].changed_flag;
  704. X  int len0 = filevec[0].buffered_lines;
  705. X  int len1 = filevec[1].buffered_lines;
  706. X  int i0 = len0, i1 = len1;
  707. X
  708. X  /* Note that changedN[-1] does exist, and contains 0.  */
  709. X
  710. X#if 0 /* Unnecessary since a line includes its trailing newline.  */
  711. X  /* In RCS comparisons, making the existence or nonexistence of trailing
  712. X     newlines really matter. */
  713. X  if (output_style == OUTPUT_RCS
  714. X      && filevec[0].missing_newline != filevec[1].missing_newline)
  715. X    changed0[len0 - 1] = changed1[len1 - 1] = 1;
  716. X#endif
  717. X
  718. X  while (i0 >= 0 || i1 >= 0)
  719. X    {
  720. X      if (changed0[i0 - 1] || changed1[i1 - 1])
  721. X    {
  722. X      int line0 = i0, line1 = i1;
  723. X
  724. X      /* Find # lines changed here in each file.  */
  725. X      while (changed0[i0 - 1]) --i0;
  726. X      while (changed1[i1 - 1]) --i1;
  727. X
  728. X      /* Record this change.  */
  729. X      script = add_change (i0, i1, line0 - i0, line1 - i1, script);
  730. X    }
  731. X
  732. X      /* We have reached lines in the two files that match each other.  */
  733. X      i0--, i1--;
  734. X    }
  735. X
  736. X  return script;
  737. X}
  738. X
  739. X/* Report the differences of two files.  DEPTH is the current directory
  740. X   depth. */
  741. Xint
  742. Xdiff_2_files (filevec, depth)
  743. X     struct file_data filevec[];
  744. X     int depth;
  745. X{
  746. X  int diags;
  747. X  int i;
  748. X  struct change *e, *p;
  749. X  struct change *script;
  750. X  int binary;
  751. X  int changes;
  752. X
  753. X  /* See if the two named files are actually the same physical file.
  754. X     If so, we know they are identical without actually reading them.  */
  755. X
  756. X  if (output_style != OUTPUT_IFDEF
  757. X      && filevec[0].stat.st_ino == filevec[1].stat.st_ino
  758. X      && filevec[0].stat.st_dev == filevec[1].stat.st_dev)
  759. X    return 0;
  760. X
  761. X  binary = read_files (filevec);
  762. X
  763. X  /* If we have detected that file 0 is a binary file,
  764. X     compare the two files as binary.  This can happen
  765. X     only when the first chunk is read.
  766. X     Also, -q means treat all files as binary.  */
  767. X
  768. X  if (binary || no_details_flag)
  769. X    {
  770. X      int differs = (filevec[0].buffered_chars != filevec[1].buffered_chars
  771. X             || bcmp (filevec[0].buffer, filevec[1].buffer,
  772. X                  filevec[1].buffered_chars));
  773. X      if (differs) 
  774. X    message (binary ? "Binary files %s and %s differ\n"
  775. X         : "Files %s and %s differ\n",
  776. X         filevec[0].name, filevec[1].name);
  777. X
  778. X      for (i = 0; i < 2; ++i)
  779. X    if (filevec[i].buffer)
  780. X      free (filevec[i].buffer);
  781. X      return differs;
  782. X    }
  783. X
  784. X  /* Allocate vectors for the results of comparison:
  785. X     a flag for each line of each file, saying whether that line
  786. X     is an insertion or deletion.
  787. X     Allocate an extra element, always zero, at each end of each vector.  */
  788. X
  789. X  filevec[0].changed_flag = (char *) xmalloc (filevec[0].buffered_lines + 2);
  790. X  filevec[1].changed_flag = (char *) xmalloc (filevec[1].buffered_lines + 2);
  791. X  bzero (filevec[0].changed_flag, filevec[0].buffered_lines + 2);
  792. X  bzero (filevec[1].changed_flag, filevec[1].buffered_lines + 2);
  793. X  filevec[0].changed_flag++;
  794. X  filevec[1].changed_flag++;
  795. X
  796. X  /* Some lines are obviously insertions or deletions
  797. X     because they don't match anything.  Detect them now,
  798. X     and avoid even thinking about them in the main comparison algorithm.  */
  799. X
  800. X  discard_confusing_lines (filevec);
  801. X
  802. X  /* Now do the main comparison algorithm, considering just the
  803. X     undiscarded lines.  */
  804. X
  805. X  xvec = filevec[0].undiscarded;
  806. X  yvec = filevec[1].undiscarded;
  807. X  diags = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
  808. X  fdiag = (int *) xmalloc (diags * sizeof (int));
  809. X  fdiag += filevec[1].nondiscarded_lines + 1;
  810. X  bdiag = (int *) xmalloc (diags * sizeof (int));
  811. X  bdiag += filevec[1].nondiscarded_lines + 1;
  812. X
  813. X  files[0] = filevec[0];
  814. X  files[1] = filevec[1];
  815. X
  816. X  compareseq (0, filevec[0].nondiscarded_lines,
  817. X          0, filevec[1].nondiscarded_lines);
  818. X
  819. X  bdiag -= filevec[1].nondiscarded_lines + 1;
  820. X  free (bdiag);
  821. X  fdiag -= filevec[1].nondiscarded_lines + 1;
  822. X  free (fdiag);
  823. X
  824. X  /* Modify the results slightly to make them prettier
  825. X     in cases where that can validly be done.  */
  826. X
  827. X  shift_boundaries (filevec);
  828. X
  829. X  /* Get the results of comparison in the form of a chain
  830. X     of `struct change's -- an edit script.  */
  831. X
  832. X  if (output_style == OUTPUT_ED)
  833. X    script = build_reverse_script (filevec);
  834. X  else
  835. X    script = build_script (filevec);
  836. X
  837. X  if (script || output_style == OUTPUT_IFDEF)
  838. X    {
  839. X      setup_output (files[0].name, files[1].name, depth);
  840. X
  841. X      switch (output_style)
  842. X    {
  843. X    case OUTPUT_CONTEXT:
  844. X      print_context_header (files, 0);
  845. X      print_context_script (script, 0);
  846. X      break;
  847. X
  848. X    case OUTPUT_UNIFIED:
  849. X      print_context_header (files, 1);
  850. X      print_context_script (script, 1);
  851. X      break;
  852. X
  853. X    case OUTPUT_ED:
  854. X      print_ed_script (script);
  855. X      break;
  856. X
  857. X    case OUTPUT_FORWARD_ED:
  858. X      pr_forward_ed_script (script);
  859. X      break;
  860. X
  861. X    case OUTPUT_RCS:
  862. X      print_rcs_script (script);
  863. X      break;
  864. X
  865. X    case OUTPUT_NORMAL:
  866. X      print_normal_script (script);
  867. X      break;
  868. X
  869. X    case OUTPUT_IFDEF:
  870. X      print_ifdef_script (script);
  871. X      break;
  872. X    }
  873. X
  874. X      finish_output ();
  875. X    }
  876. X
  877. X  /* Set CHANGES if we had any diffs that were printed.
  878. X     If some changes are being ignored, we must scan the script to decide.  */
  879. X  if (ignore_blank_lines_flag || ignore_regexp)
  880. X    {
  881. X      struct change *next = script;
  882. X      changes = 0;
  883. X
  884. X      while (next && changes == 0)
  885. X    {
  886. X      struct change *this, *end;
  887. X      int first0, last0, first1, last1, deletes, inserts;
  888. X
  889. X      /* Find a set of changes that belong together.  */
  890. X      this = next;
  891. X      end = find_change (next);
  892. X
  893. X      /* Disconnect them from the rest of the changes,
  894. X         making them a hunk, and remember the rest for next iteration.  */
  895. X      next = end->link;
  896. X      end->link = NULL;
  897. X
  898. X      /* Determine whether this hunk was printed.  */
  899. X      analyze_hunk (this, &first0, &last0, &first1, &last1,
  900. X            &deletes, &inserts);
  901. X
  902. X      /* Reconnect the script so it will all be freed properly.  */
  903. X      end->link = next;
  904. X
  905. X      if (deletes || inserts)
  906. X        changes = 1;
  907. X    }
  908. X    }
  909. X  else
  910. X    changes = (script != 0);
  911. X
  912. X  for (i = 1; i >= 0; --i)
  913. X    {
  914. X      free (filevec[i].realindexes);
  915. X      free (filevec[i].undiscarded);
  916. X    }
  917. X
  918. X  for (i = 1; i >= 0; --i)
  919. X    free (--filevec[i].changed_flag);
  920. X
  921. X  for (i = 1; i >= 0; --i)
  922. X    free (filevec[i].equivs);
  923. X
  924. X  for (i = 0; i < 2; ++i)
  925. X    {
  926. X      if (filevec[i].buffer != 0)
  927. X    free (filevec[i].buffer);
  928. X      free (filevec[i].linbuf);
  929. X    }
  930. X
  931. X  for (e = script; e; e = p)
  932. X    {
  933. X      p = e->link;
  934. X      free (e);
  935. X    }
  936. X
  937. X  if (! ROBUST_OUTPUT_STYLE (output_style)
  938. X      /* For -D, invent newlines silently.  That's ok in C code.  */
  939. X      && output_style != OUTPUT_IFDEF)
  940. X    for (i = 0; i < 2; ++i)
  941. X      if (filevec[i].missing_newline)
  942. X    {
  943. X      error ("No newline at end of file %s", filevec[i].name, "");
  944. X      changes = 2;
  945. X    }
  946. X
  947. X  return changes;
  948. X}
  949. END_OF_FILE
  950. if test 26295 -ne `wc -c <'analyze.c'`; then
  951.     echo shar: \"'analyze.c'\" unpacked with wrong size!
  952. fi
  953. # end of 'analyze.c'
  954. fi
  955. echo shar: End of archive 5 \(of 8\).
  956. cp /dev/null ark5isdone
  957. MISSING=""
  958. for I in 1 2 3 4 5 6 7 8 ; do
  959.     if test ! -f ark${I}isdone ; then
  960.     MISSING="${MISSING} ${I}"
  961.     fi
  962. done
  963. if test "${MISSING}" = "" ; then
  964.     echo You have unpacked all 8 archives.
  965.     rm -f ark[1-9]isdone
  966. else
  967.     echo You still need to unpack the following archives:
  968.     echo "        " ${MISSING}
  969. fi
  970. ##  End of shell archive.
  971. exit 0
  972.  
  973. exit 0 # Just in case...
  974.